package r.h.mobilesmsdemo;

import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.mob.MobSDK;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText etPhoneNumber;
    private EditText etCode;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_linear);

        MobSDK.init(this);

        etPhoneNumber = findViewById(R.id.etPhoneNumber);
        etCode = findViewById(R.id.etCode);

    }


    @Override
    public void onClick(View view) {
        System.out.println("Clicking!!!");
        String phone = etPhoneNumber.getText().toString().trim();
        switch (view.getId()) {
            case R.id.btnSendMsg:
                if (phone.length() == 11) {
                    System.out.println("Send to : "+phone);
                    sendCode("86", phone);
                } else{
                    Toast.makeText(this,"Length Wrong!!",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btnSubmitCode:
                String code = etCode.getText().toString().trim();
                if (code.length() == 4) {
                    submitCode("86", phone,code);
                }
                break;
        }
    }

    // 请求验证码，其中country表示国家代码，如“86”；phone表示手机号码，如“13800138000”
    public void sendCode(String country, String phone) {
        // 注册一个事件回调，用于处理发送验证码操作的结果
        SMSSDK.registerEventHandler(new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE) {
                    // TODO 处理成功得到验证码的结果
                    // 请注意，此时只是完成了发送验证码的请求，验证码短信还需要几秒钟之后才送达
//
                    System.out.println("Send Success!!");
                } else {

                    // TODO 处理错误的结果
                    System.out.println("Send Failed!!");
                }

            }
        });
        // 触发操作
        SMSSDK.getVerificationCode(country, phone);
    }

    // 提交验证码，其中的code表示验证码，如“1357”
    public void submitCode(String country, String phone, String code) {
        // 注册一个事件回调，用于处理提交验证码操作的结果
        SMSSDK.registerEventHandler(new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE) {

                    // TODO 处理验证成功的结果
                    System.out.println("Identifying Success!!");
                } else {
                    // TODO 处理错误的结果
                    System.out.println("Identifying Failed!!");
                }

            }
        });
        // 触发操作
        SMSSDK.submitVerificationCode(country, phone, code);
    }

    protected void onDestroy() {
        super.onDestroy();
        //用完回调要注销掉，否则可能会出现内存泄露
        SMSSDK.unregisterAllEventHandler();
    }

    ;
}
